package com.abocode.aichat.app.utils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/***
 *  Created by guanxf on 2015/12/16.
 */
public class StringUtil {
	public static String getNotNullStr(Object str) {
		if (str == null)
			return "";
		return str.toString();
	}

	public static boolean isNullOrEmpty(String string) {
		return string == null || "".equals(string);
	}

	/**
	 * 指示指定的字符串是 null、空还是仅由空白字符组成。
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isNullOrWhiteSpace(String value) {
		return (isNullOrEmpty(value) || value.trim().length() == 0);
	}

	public static boolean isMobile(String phoneno) {
		Pattern p = Pattern
				.compile("^((13[4-9])|(15[0-2,7-9])|(178)|(147)|(18[2-4,7-8]))\\d{8}$");  
		Matcher m = p.matcher(phoneno);
		return m.matches();
	}
	/**
	 * 资料是否合法
	 * @param phoneno
	 * @return
	 */
	public static boolean isLegitimacyNo(String phoneno) {
		Pattern p = Pattern
				.compile("(^134[0-8]\\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|18[2-478])\\d{8}$)|(^(?:13[0-2]|145|15[56]|176|18[56])\\d{8}$)|(^(?:133|153|177|18[019])\\d{8}$)|(^170([059])\\d{7}$)|(^(11\\d{7})$)");  //加入假资料11 9位
		Matcher m = p.matcher(phoneno);
		return m.matches();
	}


	/**
	 * 资料是否合法
	 * @param msisdn
	 * @return
	 */
	public static boolean isMsisdn(String msisdn) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		Matcher m = p.matcher(msisdn);
		return m.matches();
	}

	/**
	 * 判断字符串是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		for (int i = str.length();--i>=0;){
			if (!Character.isDigit(str.charAt(i))){
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断字符串是否可以转化为浮点型
	 * 
	 * @param param
	 * @return
	 */
	public static boolean isFloat(String param) {
		try {
			Float.parseFloat(param);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 判断对象或对象数组中每一个对象是否为空: 对象为null，字符序列长度为0，集合类、Map为empty
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isNullOrEmpty(Object obj) {
		if (obj == null)
			return true;

		if (obj instanceof CharSequence)
			return ((CharSequence) obj).length() == 0;

		if (obj instanceof Collection)
			return ((Collection) obj).isEmpty();

		if (obj instanceof Map)
			return ((Map) obj).isEmpty();

		if (obj instanceof Object[]) {
			Object[] object = (Object[]) obj;
			if (object.length == 0) {
				return true;
			}
			boolean empty = true;
			for (int i = 0; i < object.length; i++) {
				if (!isNullOrEmpty(object[i])) {
					empty = false;
					break;
				}
			}
			return empty;
		}
		return false;
	}

	public static int daysBetween(Date date1, Date date2) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 将异常信息转换为字符串。
	 * 
	 * @param e
	 * @return
	 */
	public static String toString(Throwable e) {
		if (e == null) {
			return "";
		}

		StringBuilder sb = new StringBuilder();
		try {
			sb.append(e.toString()).append("\r\n");
			if (e.getStackTrace() != null) {
				for (StackTraceElement st : e.getStackTrace()) {
					sb.append("\t").append(st.toString()).append("\r\n");
				}
			}
		} catch (Exception e2) {
		}
		return sb.toString();
	}

	public static String substring(String value, int end) {
		if (isNullOrEmpty(value)) {
			return "";
		}

		if (value.length() < end) {
			return value;
		}

		return value.substring(0, end);
	}
	
	public static String getRandomSize8() {
		int i=(int)(Math.random()*90000000)+10000000; 	
	    return i+"";
	}


	public static String getUUid() {
		return UUID.randomUUID().toString().replace("-","");
	}

	public static String getDateTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
		String dateTime = sdf.format(date);
		return dateTime;
	}



}